package com.tomato.lock.util;

import java.util.concurrent.*;

/**
 * 线程工具类
 */
public class TomatoThreadPoolUtil {
    public static final Integer AVAILABLE_CPU = Runtime.getRuntime().availableProcessors();

    public static final ThreadFactory TOMATO_FACTORY = new TomatoThreadFactory();

    public static final ThreadPoolExecutor THREAD_POOL = getThreadPoolExecutor();

    private static ThreadPoolExecutor getThreadPoolExecutor() {
        //  IO密集型线程池  核心线程数为
        ThreadPoolExecutor executor =
                new ThreadPoolExecutor(
                        AVAILABLE_CPU * 2 + 1,
                        AVAILABLE_CPU * 2 + 1,
                        0,
                        TimeUnit.SECONDS,
                        new LinkedBlockingDeque<>(65535),
                        TOMATO_FACTORY,
                        new ThreadPoolExecutor.AbortPolicy());

        // 准备启动所有的和弦线程
        executor.prestartAllCoreThreads();
        return executor;

    }

}
